package org.jdemo.tools.bookmark;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.SimpleBookmark;
import org.jdemo.util.IOUtil;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;

/**
 * BookmarkImporter
 *
 * @author longlin(longlin@cyou-inc.com)
 * @date 2014-3-10
 * @since V1.0
 */
public class BookmarkImporter {
    public static void main(String[] args) throws Exception {
//        importBookmark("F:/iPad/__/add/origin/Hadoop实战 第2版.pdf", "F:/iPad/__/add/new/Hadoop实战 第2版.pdf", "F:/iPad/__/bookmark/Hadoop实战（第2版）.txt", "gbk", 19);
//        importBookmark("F:/iPad/__/add/origin/lucene实战 第2版.pdf", "F:/iPad/__/add/new/lucene实战 第2版.pdf", "F:/iPad/__/bookmark/lucene实战 第2版.txt", "gbk", 11);
//        importBookmark("F:/iPad/__/add/origin/大数据-互联网大规模数据挖掘与分布式处理.pdf", "F:/iPad/__/add/new/大数据-互联网大规模数据挖掘与分布式处理.pdf", "F:/iPad/__/bookmark/大数据-互联网大规模数据挖掘与分布式处理.txt", "gbk", 8);
//        importBookmark("F:/iPad/__/add/origin/推荐系统实践.pdf", "F:/iPad/__/add/new/推荐系统实践.pdf", "F:/iPad/__/bookmark/推荐系统实践.txt", "gbk", 17);
//        importBookmark("F:/iPad/__/add/origin/云计算宝典：技术与实践.虚拟化与云计算.pdf", "F:/iPad/__/add/new/云计算宝典：技术与实践.虚拟化与云计算.pdf", "F:/iPad/__/bookmark/云计算宝典：技术与实践.虚拟化与云计算.txt", "gbk", 14);
//        importBookmark("F:/iPad/__/add/origin/Java并发编程实战.pdf", "F:/iPad/__/add/new/Java并发编程实战.pdf", "F:/iPad/__/bookmark/Java并发编程实战.txt", "gbk", 15);
//        importBookmark("F:/iPad/__/add/origin/C.GUI.Qt.4编程(第二版).pdf", "F:/iPad/__/add/new/C.GUI.Qt.4编程(第二版).pdf", "F:/iPad/__/bookmark/C.GUI.Qt.4编程(第二版).txt", "gbk", 15);
//        importBookmark("F:/iPad/__/add/origin/Python 3程序开发指南（第二版）.pdf", "F:/iPad/__/add/new/Python 3程序开发指南（第二版）.pdf", "F:/iPad/__/bookmark/Python 3程序开发指南（第二版）.txt", "gbk", 16);
//        importBookmark("F:/iPad/__/add/origin/AngularJS中文版.pdf", "F:/iPad/__/add/new/AngularJS中文版.pdf", "F:/iPad/__/bookmark/AngularJS中文.txt", "gbk", 0);
//        importBookmark("F:/iPad/__/add/origin/Linux命令、编辑器与shell编程(第二版).pdf", "F:/iPad/__/add/new/Linux命令、编辑器与shell编程(第二版).pdf", "F:/iPad/__/bookmark/Linux命令、编辑器与Shell编程.txt", "gbk", 19);
//        importBookmark("F:/iPad/__/add/origin/飞天开放平台编程指南：阿里云计算的实践.pdf", "F:/iPad/__/add/new/飞天开放平台编程指南：阿里云计算的实践.pdf", "F:/iPad/__/bookmark/飞天开放平台编程指南：阿里云计算的实践.txt", "gbk", 0);
//        importBookmark("F:/iPad/__/add/origin/数据挖掘应用20个案例分析.pdf", "F:/iPad/__/add/new/数据挖掘应用20个案例分析.pdf", "F:/iPad/__/bookmark/数据挖掘应用20个案例分析.txt", "gbk", 0);
//        importBookmark("F:/iPad/__/add/origin/大数据-互联网大规模数据挖掘与分布式处理.pdf", "F:/iPad/__/add/new/大数据-互联网大规模数据挖掘与分布式处理.pdf", "F:/iPad/__/bookmark/大数据-互联网大规模数据挖掘与分布式处理.txt", "gbk", 8);
//        importBookmark("F:/iPad/__/add/origin/HTML5与CSS3权威指南.pdf", "F:/iPad/__/add/new/HTML5与CSS3权威指南.pdf", "F:/iPad/__/bookmark/HTML5与CSS3权威指南.txt", "gbk", 16);
//        importBookmark("F:/iPad/__/add/origin/推荐系统实践.pdf", "F:/iPad/__/add/new/推荐系统实践.pdf", "F:/iPad/__/bookmark/推荐系统实践.txt", "gbk", 17);
//        importBookmark("F:/iPad/__/add/origin/深入理解Java虚拟机-JVM高级特性与最佳实践.pdf", "F:/iPad/__/add/new/深入理解Java虚拟机-JVM高级特性与最佳实践.pdf", "F:/iPad/__/bookmark/深入理解Java虚拟机-JVM高级特性与最佳实践.txt", "gbk", 21);
//        importBookmark("F:/iPad/__/add/origin/分布式JAVA应用 基础与实践.pdf", "F:/iPad/__/add/new/分布式JAVA应用 基础与实践.pdf", "F:/iPad/__/bookmark/分布式JAVA应用 基础与实践.txt", "gbk", 16);
//        importBookmark("F:/iPad/__/add/origin/Java网络编程精解.pdf", "F:/iPad/__/add/new/Java网络编程精解.pdf", "F:/iPad/__/bookmark/Java网络编程精解.txt", "gbk", 14);
        importBookmark("F:/iPad/__/add/origin/Java并发编程学习笔记.pdf", "F:/iPad/__/add/new/Java并发编程学习笔记.pdf", "F:/iPad/__/bookmark/Java并发编程学习笔记.txt", "gbk", 4);
    }

    public static void importBookmark(String srcPdf, String destPdf, String bookmarkFile, String bookmarkEncoding, int addPageCount) throws Exception {
        IOUtil.deleteFile(destPdf);
        List<Bookmark> bookmarks = BookmarkFactory.buildBookmarks(IOUtil.readFile(bookmarkFile, bookmarkEncoding));
        String bookmarkXML = toBookmarkXML(bookmarks, addPageCount);
        List<HashMap<String, Object>> outlines = SimpleBookmark.importFromXML(new ByteArrayInputStream(bookmarkXML.getBytes()));
        PdfReader reader = new PdfReader(srcPdf);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destPdf));
        stamper.setOutlines(outlines);
        stamper.close();
        reader.close();
        System.out.println(bookmarkXML);
    }

    public static String toBookmarkXML(List<Bookmark> bookmarks, int addPageCount) {
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        xml += "\n<Bookmark>";
        for (Bookmark bookmark : bookmarks) {
            xml += bookmark.toXML(addPageCount) + "\n";
        }
        xml += "\n</Bookmark>";
        return xml;
    }
}
